module.exports = function ($scope, $resource, FileUploader, toaster, date2str) {

	$scope.company = {};
	$scope.date = {
        //'lable': date2str(new Date()),
		'opened': false,
		'opened1': false
	}

	$resource('/api/as/goc/enterpriserecord/enterpriseinfo', {}, {}).save({}, function(res){
		if (res.errcode === 0) {
			$scope.company = res.data;
			$scope.date.lable = res.data.company_start_date;
			$scope.date.lable1 = res.data.company_end_date;
		}
	});

	//保存
	$scope.save = function() {
		$scope.company.company_start_date = date2str($scope.date.lable);
		$scope.company.company_end_date = date2str($scope.date.lable1);
		if(!checkInfo($scope.company)) return;
		$resource('/api/as/goc/enterpriserecord/createenterprise', {}, {}).save($scope.company, function(res){
            if (res.errcode === 0) {
				toaster.success({title: '', body: '保存成功'});
            } else {
                toaster.error({title: '', body: res.errmsg});
            }
        });
	}

	//上传图片
	var uploader = $scope.uploader = new FileUploader({
        url: 'https://txy.juyouhx.com/Api/Api/ObjectToOss?topdir=aa&selfdir=bb'
    });
    uploader.filters.push({
        name: 'imageFilter',
        fn: function(item /*{File|FileLikeObject*/, options) {
            var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
            return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
        }
	});
	uploader.onSuccessItem = function(fileItem, response, status, headers) {
        $scope.company.business_license_img_url = response.savename;
    };

	//校验企业信息
	function checkInfo(company){
		if(!company.company_code){
			toaster.warning({title: '', body: '请填写统一社会信用代码.'});
			return false;
		}
		if(!company.business_license_code){
			toaster.warning({title: '', body: '请填写营业执照编号.'});
			return false;
		}
		if(!company.registered_address){
			toaster.warning({title: '', body: '请填写企业注册地址.'});
			return false;
		}
		if(!company.company_type){
			toaster.warning({title: '', body: '请填写公司类型.'});
			return false;
		}
		if(!company.registered_capital){
			toaster.warning({title: '', body: '请填写注册资金汉字大写.'});
			return false;
		}
		if(!company.company_start_date){
			toaster.warning({title: '', body: '请填写成立时间.'});
			return false;
		}
		if(!company.company_end_date){
			toaster.warning({title: '', body: '请填写营业期限.'});
			return false;
		}
		if(!company.phone){
			toaster.warning({title: '', body: '请填写公司电话、传真.'});
			return false;
		}
		if(!company.company_email){
			toaster.warning({title: '', body: '请填写公司邮箱.'});
			return false;
		}
		if(!company.responsible_name){
			toaster.warning({title: '', body: '请填写负责人名字.'});
			return false;
		}
		if(!company.responsible_no){
			toaster.warning({title: '', body: '请填写负责人身份证号.'});
			return false;
		}
		if(!company.responsible_mobile){
			toaster.warning({title: '', body: '请填写负责人手机.'});
			return false;
		}
		if(!/^1[3|5|7|8|9]\d{9}$/.test(company.responsible_mobile)){
			toaster.warning({title: '', body: '手机号格式不正确'});
			return false;
		}
		if(!company.contact_address){
			toaster.warning({title: '', body: '请填写联系地址.'});
			return false;
		}
		if(!company.juridical_person){
			toaster.warning({title: '', body: '请填写法人.'});
			return false;
		}
		if(!company.business_scope){
			toaster.warning({title: '', body: '请填写经营范围.'});
			return false;
		}
		if(!company.business_license_img_url){
			toaster.warning({title: '', body: '请上传营业执照图片.'});
			return false;
		}
		return true;
	}

	$scope.dateOpen = function ($event, item) {
		console.log('open');
		$event.preventDefault();
		$event.stopPropagation();
		item.opened = true;
	};

	$scope.dateOpen1 = function ($event, item) {
		console.log('open');
		$event.preventDefault();
		$event.stopPropagation();
		item.opened1 = true;
	};

};

